import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()],
  },
  preload: {
    plugins: [externalizeDepsPlugin()],
  },
  renderer: {
    root: './src/renderer',
    server: {
      port: 8174,
    },
    resolve: {
      alias: {
        '@': resolve('src/renderer/src'),
      },
    },
    plugins: [
      vue(),
      AutoImport({
        imports: [
          'vue',
          'vue-router',
          {
            'element-plus': ['ElMessage', 'ElMessageBox'],
          },
          {
            '@/WeatherEarth/Source/WeatherEarth.js': [['*', 'WeatherEarth']],
          },
          {
            '@/WeatherEarth/Source/WeatherEarth.js': ['Cesium'],
          },
        ],
        dts: 'src/types/auto-imports.d.ts',
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        dts: 'src/types/components.d.ts',
        dirs: ['src/components', 'src/we-api/we-components'],
        extensions: ['vue'],
        include: [/\.vue$/, /\.vue\?vue/],
        exclude: [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/],
      }),
      viteStaticCopy({
        targets: [
          {
            src: '../../node_modules/cesium/Build/Cesium/Assets',
            dest: 'Cesium/',
          },
          {
            src: '../../node_modules/cesium/Build/Cesium/ThirdParty',
            dest: 'Cesium/',
          },
          {
            src: '../../node_modules/cesium/Build/Cesium/Widgets',
            dest: 'Cesium/',
          },
          {
            src: '../../node_modules/cesium/Build/Cesium/Workers',
            dest: 'Cesium/',
          },
          {
            src: 'src/we-api/we-assets',
            dest: 'Cesium/',
          },
          {
            src: 'src/WeatherEarth/Build/we-assets',
            dest: 'Cesium/',
          },
        ],
      }),
    ],
    css: {
      postcss: './postcss.config.js',
    },
    build: {
      rollupOptions: {
        output: {
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
        },
      },
    },
  },
})
